﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Infragistics.Windows.DataPresenter;
using System.Collections;
using System.ComponentModel;
using Ooganizer.API;
using Ooganizer.Server;

namespace Ooganizer.Client.UI.WorkspaceSelectorLib
{
    /// <summary>
    /// Interaction logic for WorkspaceSelectorWindow.xaml
    /// </summary>
    public partial class WorkspaceSelector : UserControl
    {
        public WorkspaceSelector()
        {
            InitializeComponent();

            m_sortDesc = new SortDescription("Name", ListSortDirection.Descending);
        }

        //void OnComboSelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    this.SetReversedText();
        //}

        void OnSortComboSelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            if(frame1 != null)
            {
                WorkspaceGrid grid = (WorkspaceGrid)(frame1.Content);
                
                switch(e.AddedItems[0].ToString())
                {
                    case "SortByNameDescending":
                        m_sortDesc = new SortDescription("Name", ListSortDirection.Descending);
                        //grid.DataPresenter1.DataSource = ((AutoRefreshCollectionViewSource)frame1.Resources["SortByNameDescending"]).;
                        break;
                    case "SortByNameAscending":
                        m_sortDesc = new SortDescription("Name", ListSortDirection.Ascending);                        
                        //grid.DataPresenter1.DataSource = (AutoRefreshCollectionViewSource)frame1.Resources["SortByNameAscending"];
                        break;
                    default:
                        break;
                }

                ICollectionView collectionView = CollectionViewSource.GetDefaultView(grid.DataPresenter1.DataSource);
                collectionView.SortDescriptions.Clear();
                collectionView.SortDescriptions.Add(m_sortDesc);
                collectionView.Refresh();
            }
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            AddNewWorkspace addNewWorkspaceDialog = new AddNewWorkspace();
            addNewWorkspaceDialog.ShowDialog();

            // Refresh the data and view
            WorkspaceGrid grid = (WorkspaceGrid)(frame1.Content);

            ObjectDataProvider dataProvider = (ObjectDataProvider)grid.Resources["odpObjectDataProvider1"];
            dataProvider.Refresh();

            //ICollectionView collectionView = CollectionViewSource.GetDefaultView(grid.DataPresenter1.DataSource);
            //collectionView.SortDescriptions.Clear();
            //collectionView.SortDescriptions.Add(m_sortDesc);
            //collectionView.Refresh();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            WorkspaceGrid grid = (WorkspaceGrid)(frame1.Content);

            using (OoganizerState oogyState = new OoganizerState())
            {
                using (OoganizerAPIProxy proxy = oogyState.GetAPIProxy())
                {
                    // Handle multiple selection
                    foreach(var cell in grid.DataPresenter1.SelectedItems.Cells)
                    {                            
                        string selectedWorkspaceName = cell.Value.ToString();

                        if (selectedWorkspaceName != null)
                        {
                            proxy.API.DeleteWorkspace(selectedWorkspaceName);
                        }
                    }
                }
            }

            // Refresh the data and view
            ObjectDataProvider dataProvider = (ObjectDataProvider)grid.Resources["odpObjectDataProvider1"];
            dataProvider.Refresh();

            //ICollectionView collectionView = CollectionViewSource.GetDefaultView(grid.DataPresenter1.DataSource);
            //collectionView.SortDescriptions.Clear();
            //collectionView.SortDescriptions.Add(m_sortDesc);
            //collectionView.Refresh();
        }

        private SortDescription m_sortDesc;
    }
}
